/**
 * 时间格式取年月日
 * @param time
 * @returns {*}
 */
function getYearMonthDate(time) {
    var YMD = time.split(" ")[0];  // 年月日
    var HMS = time.split(" ")[1];  // 时分秒
    return YMD;
}
layui.config({
    base: '/static/js/layuiModulesEX/'
});
layui.use(['paging', 'form', 'upload'], function () {
    var $ = layui.jquery,
        paging = layui.paging(),
        layer = layui.layer, //获取当前窗口的layer对象
        form = layui.form();
    var image = {};

    //首先渲染
    form.render();
    paging.init({
        openWait: true,
        url: '/recruitInfo/findByCondition',
        elem: '#content', //内容容器
        params: { //发送到服务端的参数
            publisherid: userId
        },
        contentType: "application/json; charset=utf-8",
        type: 'POST',
        tempElem: '#tpl', //模块容器
        pageConfig: { //分页参数配置
            elem: 'paged', //分页容器
            pageSize: 10 //分页大小
        },
        success: function () { //渲染成功的回调
            //alert('渲染成功');
        },
        fail: function (msg) { //获取数据失败的回调
            //alert('获取数据失败')
        },
        complate: function () { //完成的回调
            //alert('处理完成');
            //绑定所有编辑按钮事件
            $('#content').children('tr').each(function () {
                //获取每行记录的id值
                var $that = $(this).children('td:last-child');
                var id = $that.children('a[data-opt=delete]').data('id');
                //编辑
                $that.children('a[data-opt=edit]').on('click', function () {
                    location.href='/enterprise/addrecruitinfo?recruitInfoId='+id;
                    // axios.get('/recruitInfo/find/' + id)
                    //     .then(function (response) {
                    //         // console.log(response.data.data);
                    //         vm.recruitinfo = response.data.data;
                    //         form.render('select');
                    //         $('#Vue input[type=file]').show();
                    //         $('select[name=type]').next().find('[lay-value="' + vm.recruitinfo.type + '"]').click();
                    //         $('select[name=positiontype]').next().find('[lay-value="' + vm.recruitinfo.positiontype + '"]').click();
                    //         $('#Vue input[value="提交"]').show();
                    //         $('#Vue input[value="暂存"]').show();
                    //         var divshow = $("#job");
                    //         divshow.text("");// 清空数据
                    //         console.log(vm.recruitinfo);
                    //         divshow.html(vm.recruitinfo.job);
                    //         if (vm.recruitinfo.workingpic === 0) {
                    //             $('#showWorkingPic').attr('src', "");
                    //             vm.show = false;
                    //         }
                    //         else {
                    //             axios.get("/image/find/" + vm.recruitinfo.workingpic)
                    //                 .then(function (response) {
                    //                     $('#showWorkingPic').attr('src', response.data.data);
                    //                     vm.show = false;
                    //                 })
                    //                 .catch(function (response) {
                    //                     console.log(response);
                    //                 })
                    //         }
                    //
                    //     })
                    //     .catch(function (response) {
                    //         console.log(response);
                    //     });
                });
                //删除
                $that.children('a[data-opt=delete]').on('click', function () {
                    layer.confirm('确认删除？', function (index) {
                        axios.get('/recruitInfo/remove/' + id)
                            .then(function (response) {
                                console.log(response);
                            })
                            .catch(function (response) {
                                console.log(response);
                            });
                        layer.close(index);
                        location.reload();
                    });

                });
                // 查看
                $that.children('a[data-opt=more]').on('click',function () {
                    axios.get('/recruitInfo/find/' + id)
                        .then(function (response) {
                            // console.log(response.data.data);
                            vm.recruitinfo = response.data.data;
                            vm.recruitinfo.positiontype = positionTypes[vm.recruitinfo.positiontype];
                            form.render('select');
                            var divshow = $("#hello");
                            divshow.text("");// 清空数据
                            console.log(vm.recruitinfo);
                            divshow.html(vm.recruitinfo.job);
                            if (vm.recruitinfo.workingpic === 0) {
                                $('#showWorkingPic').attr('src', "");
                                vm.show = false;
                            }
                            else {
                                axios.get("/image/find/" + vm.recruitinfo.workingpic)
                                    .then(function (response) {
                                        $('#showWorkingPic').attr('src', response.data.data);
                                        vm.show = false;
                                    })
                                    .catch(function (response) {
                                        console.log(response);
                                    })
                            }

                        })
                        .catch(function (response) {
                            console.log(response);
                        });
                })
            });

        }
    });
    var vm = new Vue({
        el: '#Vue',
        data: {
            show: true,
            recruitinfo: {
                publisherid: userId
            }
        },

        methods: {
            /**
             * 添加按钮
             */
            add:function () {
                axios.get('/orginfo/findByManagerId/'+userId)
                    .then(function (response) {
                        console.log(response.data);
                        if (response.data.data.orgInfo.state == 3){
                            window.location='/enterprise/addrecruitinfo';
                        }
                        else{
                            layer.msg('审核暂时未通过！不能添加招聘信息！',{time:2000});
                        }
                    })
                    .catch(function (response) {
                        console.log(response);
                    });
            },

            /**
             * 暂存按钮
             */
            temporarilySaved: function () {
                vm.recruitinfo.state = 1;//未提交
                vm.saveOrUpdate();
            },
            /**
             * 提交按钮
             */
            submit: function () {
                vm.recruitinfo.state = 2;//待审核
                vm.saveOrUpdate();
            },
            /**
             * Todo 查询按钮
             */
            search: function () {

            },
            /**
             * 返回按钮
             */
            reload: function () {
                paging.get();
                vm.show = true;
                vm.clearData();
            },
            /**
             * 插入或更新数据库记录
             */
            saveOrUpdate: function () {
                var url = vm.recruitinfo.id == null ? "/recruitInfo/add" : "/recruitInfo/modify";
                axios.post(url, vm.recruitinfo)
                    .then(function (response) {
                        layer.msg(response.data.message, {time: 1500}, function () {
                            if (response.data.success) {
                                vm.reload();
                                vm.clearData();
                            }
                        });
                    })
                    .catch(function (response) {
                        console.log(response);
                        vm.clearData();
                    });
            },
            /**
             * 清除脏数据
             */
            clearData: function () {
                //重置数据并清除脏数据 默认为提交状态
                vm.recruitinfo = {state: 2, publisherid: userId};
                //清除照片src路径
                $('#showWorkingPic').attr('src', "");
                //重新渲染表单
                form.render();
            }
        },

    });
});
